package com.angel.provider.health.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.angel.core.Constants;
import com.angel.core.base.BaseProviderImpl;
import com.angel.core.util.CodeUtil;
import com.angel.core.util.Pager;
import com.angel.dao.health.BuzDepartmentMapper;
import com.angel.dao.health.BuzMedicalMapper;
import com.angel.dao.health.SysAreaMapper;
import com.angel.health.BuzDepartment;
import com.angel.health.BuzFranchisee;
import com.angel.health.BuzMedical;
import com.angel.health.SysArea;
import com.angel.provider.health.BuzMedicalProvider;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class BuzMedicalProviderImpl extends BaseProviderImpl<BuzMedical> implements BuzMedicalProvider {

	@Autowired
	private BuzMedicalMapper mapper;
	
	@Autowired
	private SysAreaMapper sysAreaMapper;
	
	@Autowired
	private BuzDepartmentMapper buzDepartmentMapper;
	
	public BuzMedical findById(Integer id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public Pager findAll(BuzMedical record, Pager pager) {
		PageHelper.startPage(pager.getCurrentPage(), pager.getPageSize());
		
		List<Map<String, Object>> list = mapper.findAllByParam(record, pager);
		
		PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(list);
		
		pager.setTotal(pageInfo.getTotal());
		pager.setTotalPages(pageInfo.getPages());
		pager.setList(pageInfo.getList());
		return pager;
	}
	
	@Override
	@Transactional
	public int insert(BuzMedical record) {
		int id = super.insert(record);
		
		//插入科室
		List<BuzDepartment> departs = record.getDepart();
		for(BuzDepartment depart : departs){
			if(depart.getName() != null){
				depart.setCreateTime(new Date());
				depart.setCreateBy(record.getCreateBy());
				depart.setMedicalId(record.getId());
				buzDepartmentMapper.insertSelective(depart);
			}
		}
		
		// 编码规则 省 市 年份 业务 id
		Integer provincesId = record.getProvinces();
		Integer cityId = record.getCity();
		
		String areaCode = "000000";
		
		SysArea sysArea = null;
		
		if(provincesId != null && cityId == null){
			sysArea = sysAreaMapper.selectByPrimaryKey(provincesId);
		}else if(provincesId != null && cityId != null){
			sysArea = sysAreaMapper.selectByPrimaryKey(cityId);
		}
		
		if(sysArea != null)
			areaCode = sysArea.getCode();
		
		//生成编码
		String code = CodeUtil.createCode(areaCode, Constants.MEDICAL, record.getId());
		
		record.setCode(code);
		mapper.updateByPrimaryKeySelective(record);
		
		return id;
	}

	@Override
	@Transactional
	public int updateById(BuzMedical record) {
		int i = super.updateById(record);
		
		//删除科室
		buzDepartmentMapper.deleteByMedicalId(record.getId());
		
		//插入科室
		List<BuzDepartment> departs = record.getDepart();
		for(BuzDepartment depart : departs){
			if(depart.getName() != null){
				depart.setCreateTime(new Date());
				depart.setCreateBy(record.getCreateBy());
				depart.setMedicalId(record.getId());
				buzDepartmentMapper.insertSelective(depart);
			}
		}
		
		return i;
	}

	@Override
	public List<BuzMedical> selectAllMedicalAndDepartment(BuzMedical record) {
		
		return mapper.selectAllMedicalAndDepartment(record);
		
	}
	
	
}








